package com.kuang.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kuang.common.err.ResultCodeEnum;
import com.kuang.common.err.ValidationException;
import com.kuang.entity.User;
import com.kuang.mapper.UserMapper;
import com.kuang.vo.WeixinDataVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    private final static Logger log = LoggerFactory.getLogger(UserServiceImpl.class);

    /**
     * 用户登陆使用
     *
     * @param username
     * @return
     */
    public User getByUserName(String username) {
        if (StringUtils.isEmpty(username)) {
            throw new ValidationException(ResultCodeEnum.EMPTY_ERROR);
        }
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUsername, username);
        return this.getOne(lambdaQueryWrapper);
    }


    /**
     * 根据openid获取用户信息
     *
     * @param openid
     * @return
     */
    public User getUserByOpenid(String openid) {
        if (StringUtils.isEmpty(openid)) {
            throw new ValidationException(ResultCodeEnum.EMPTY_ERROR);
        }
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getOpenid, openid);
        return this.getOne(lambdaQueryWrapper);
    }


    /**
     * 根据用户Id查询用户恓
     *
     * @param userId 用户id
     * @return
     */
    public User getUserById(Long userId) {
        if (userId == null) {
            throw new ValidationException(ResultCodeEnum.EMPTY_ERROR);
        }
        return this.getById(userId);
    }


    /**
     * 根据微信用户信息注册用户
     *
     * @param weixinDataVo
     * @return
     */
    public User wexinRegUser(WeixinDataVo weixinDataVo) {
        log.info("注册的用户信息是：{}",weixinDataVo);
        // 1:判断对象是否为空，如果为空直接返回
        if (StringUtils.isEmpty(weixinDataVo)) {
            throw new ValidationException(ResultCodeEnum.EMPTY_ERROR);
        }
        // 2:判断openid是否为空，如果为空直接返回
        if (StringUtils.isEmpty(weixinDataVo.getOpenid())) {
            throw new ValidationException(ResultCodeEnum.EMPTY_ERROR);
        }

        // 3: 根据用户openid查询用户是否注册，如果注册直接返回
        User user = this.getUserByOpenid(weixinDataVo.getOpenid());
        if (user == null) {
            // 4: 如果没有注册，就进行注册操作
            user = new User();
            user.setUsername(weixinDataVo.getNickName());
            user.setPassword("");
            user.setOpenid(weixinDataVo.getOpenid());
            user.setAvatarUrl(weixinDataVo.getAvatarUrl());
            user.setCity(weixinDataVo.getCity());
            user.setCountry(weixinDataVo.getCountry());
            user.setGender(weixinDataVo.getGender());
            user.setNickName(weixinDataVo.getNickName());
            user.setProvince(weixinDataVo.getProvince());
            user.setActive(1);
            // 保存入库,在这里保存成功，mybatis会自动回填用户id到user对象中
            this.saveOrUpdate(user);
        }
        // 5 :直接返回用户信息
        return user;
    }
}
